package filter

import (
	"adam2/internal/model"
	iUitl "adam2/internal/util"
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 过滤器：去除一字涨停版
type OneWordLimitUpFilter struct {
	db *gorm.DB
}

// 初始化
func (o *OneWordLimitUpFilter) Init(db *gorm.DB) {
	o.db = db
}

// 执行
func (o *OneWordLimitUpFilter) DoFilter(transactionDate string) {
	io.Infoln("过滤器：去除一字涨停版，交易日期[%s]", transactionDate)

	var quantStockFilterArray model.QuantStockFilterArray
	o.db.Raw("select * from quant_stock_filter").Scan(&quantStockFilterArray)

	for _, quantStockFilter := range quantStockFilterArray {
		var stockTransactionDataAll model.StockTransactionDataAll
		o.db.Raw("select * from stock_transaction_data_all t "+
			"where t.code_ = ? and t.date_ = to_date(?, 'yyyy-mm-dd')",
			quantStockFilter.StockCode, transactionDate).Scan(&stockTransactionDataAll)

		// 如果当天停牌，则删除这只股票
		if stockTransactionDataAll.ID == 0 {
			o.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", quantStockFilter.StockCode)
			continue
		}

		// 如果是一字涨停板，则删除
		if iUitl.OneWordLimitUp(stockTransactionDataAll, transactionDate) {
			o.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", quantStockFilter.StockCode)
		}
	}
}
